<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 满送活动
 * @author Scor 2015/09/30
 *
 */
class BuySendController extends AdminController {
      public function index(){
          $res=M("buy_send")->select();
          $this->assign("activities",$res);
          $this->display();
      }
    public  function insert(){
        $arr=I();
        if(count($arr)>0){
            $i =count($arr['require']);
            $arr1=Array();
            $flag = false;
            $result=0;
            for($j=0;$j<$i;$j++){
                if($arr['id']&&$arr['require']&&$arr["send"]){
                    $map['id']=$arr['id'][$j];
                    $data1['require']=$arr['require'][$j];
                    $data1['send']=$arr['send'][$j];
                    $result+=D('activity_more_buy')->where($map)->save($data1);
                    continue;
                }
                if(!$arr['require']||!$arr["send"]){
                    $flag=true;
                    continue;
                }
                $data['require']=$arr['require'][$j];
                $data['send']=$arr['send'][$j];
                array_push($arr1,$data);
            }
            $res= D('buy_send')->addAll($arr1);
            if($result||$res){
                if($flag){
                    $this->success("修改成功,但部分商品优惠价格或数量未设置!", U('BuySend/index'),1);
                }else{
                    $this->success("修改成功!", U('BuySend/index'),1);
                }
            }else{
                if($flag){
                    $this->error("修改失败,部分商品优惠价格或数量未设置!", U('BuySend/index'),1);
                }else{
                    $this->error("修改失败!", U('BuySend/index'),1);
                }

            }
        }else{
            $this->success("没有新数据！", U('BuySend/index'),1);
        }


    }
    //
    public function deleteById($id){
        $data['id']=$id;
        $res=D('activity_more_buy')->where($data)->delete();
        if($res){
            $this->success("删除成功!");
        }else{
            $this->error("删除失败！");
        }
    }
}
